<template>
	<view>
		<st-mc-framework :navTitle="ModuleTitle" @changeLevel="changeLevel"
			@AddModuleData_select='AddModuleData_select'>
			<template #workbench>
				<m-drag-button :style="{ opacity: BtnShow ? 1 : 0 }" :isDock="true" :existTabBar="true"
					class="m-drag-button-view" :initialBottom="110">
					<template #BtnContent>
						<u-icon name="search" color="#fff" size="25px" @click="search"></u-icon>
					</template>
				</m-drag-button>
			</template>
			<!-- 主内容 -->
			<template #content>
				<view class="content_box f-dc">
					<view class="table table_top_th">
						<table>
							<tr>
								<th width='15%'>单据编号</th>
								<th width='16%'>单据日期</th>
								<th width='16%'>派工单编号</th>
								<th width='18%'>产品名称</th>
								<th width='8%'>申请人</th>
								<th width='19%'>异常原因</th>
								<th width='8%'>审核人</th>
							</tr>
						</table>
					</view>
					<z-paging ref="paging" v-model="contents" @query="queryList" :fixed="false" class="z_paging">
						<view class="table">
							<table style="table-layout: fixed;">
								<tbody>
									<tr v-for="(item,index) in contents" @click="planIDClick(item,index)"
										:class="{ hovered: hoveredIndex === index }">

										<td width='15%'>
											<view class="newlineDiv"><text>{{item.BillNO}}</text></view>
										</td>
										<td width='16%'>
											<view class="newlineDiv"><text>{{item.BillDate}}</text></view>
										</td>
										<td width='16%'>
											<view class="newlineDiv"><text>{{item.PGDBillNO}}</text></view>
										</td>
										<td width='18%'>
											<view class="newlineDiv"><text>{{item.ProductName}}</text></view>
										</td>
										<td width='8%'>
											<view class="newlineDiv"><text>{{item.EmplName}}</text></view>
										</td>
										<td width='19%'>
											<view class="newlineDiv"><text>{{item.NGCause}}</text></view>
										</td>
										<td width='8%'>
											<view class="newlineDiv"><text>{{item.Approver}}</text></view>
										</td>
									</tr>
								</tbody>

							</table>

						</view>

					</z-paging>

				</view>

				<uni-popup ref="popup" type="bottom" background-color="#fff" border-radius="10px 10px 0 0">
					<view class="popup_content">

						<u-cell-group :border='false'>
					<!-- 		<u-cell title="产品" :isLink="false" :value='FormData.ProductName'
								@click="openBaseScreen('产品')"></u-cell> -->
							<u-cell title="开始时间" :isLink="false" :value='FormData.FBDate'
								@click="addTime('开始')"></u-cell>
							<u-cell title="结束时间" :isLink="false" :value='FormData.FEDate'
								@click="addTime('结束')"></u-cell>
						</u-cell-group>
						<view class="bottomBtns">
							<button block @click="submit">
								查 &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 询
							</button>
						</view>

					</view>
				</uni-popup>
			</template>
		</st-mc-framework>
		<u-datetime-picker :show="show" v-model="value1" mode="date" @cancel='show = false'
			@confirm='pickerConfirm'></u-datetime-picker>
		<m-loading :text="loadingName" v-model="showMLoading" :showClose="false"
			maskBackgroundColor="rgba(0,0,0,0.3)"></m-loading>
	</view>
</template>

<script>
	import {
		getTimeStamp,
		getFormatDateTime,
		getBefore7DateTime,
		formatDateTime,
		navigateMixin,
		baseAddModuleData_select,
		getchangeLevel
	} from '@/st_mes_zs/common/base.js';

	export default {
		mixins: [navigateMixin, baseAddModuleData_select],
		data() {
			return {
				FormData: {

					PageIndex: 1,
					PageSize: 30,
					FBDate: '',
					FEDate: '',
					DeviceID: 0,
					DeviceIDName: 0,
					EquipmentName: '全部',
					BillType: 'PZYC',
					ProductID: 0,
					ProductName: '全部',
					getCP: ''
				},
				contents: [],
				show: false,
				value1: Number(new Date()),
				TimeName: '',
				hoveredIndex: null,
				jobid: null,
				ProcedureList: [],
				disabled: false,
				ModuleTitle: '', //标题名称
				showMLoading: false, //是否加载
				loadingName: '正在加载中……', //加载名称
				BtnShow: false, //按钮显示
				FWorkshopID: 0,
			};
		},
		onShow() {
			uni.$on('refreshDetail', (res) => {
				this.getValue(res); // 调用目标方法
			});
			uni.$on('refreshList', () => {
				this.getList(); // 调用目标方法
			});
		},
		onUnload() {
			uni.$off('refreshList');
			uni.$off('refreshDetail');
		},
		onReady() {
			var that = this
			uni.hideLoading();
			that.showMLoading = true
			const {
				GuyueModule,
				UserInfo,
				appConfigInfo
			} = that.$store.state;
			const {
				MesBasicInfo
			} = GuyueModule;
			that.FWorkshopID = MesBasicInfo.FWorkshopID;
			that.ModuleTitle = appConfigInfo.ActiveModule.ModuleName

			that.$nextTick(() => {
				setTimeout(() => {
					that.BtnShow = true;
				}, 500)
			})
		},
		onLoad(options) {
			var that = this
			that.$safeStatus({
				payload: '其他', // 要传递的数据
			});
			const {
				GuyueModule,
				UserInfo,
				appConfigInfo
			} = that.$store.state;
			const {
				MesBasicInfo
			} = GuyueModule;
			if (options.Dispatchitem) {
				const dispatchItem = JSON.parse(options.Dispatchitem);
				that.assignFormData(that.FormData, dispatchItem);
				return;
			}
			const formDefaults = {
				EquipmentID: MesBasicInfo.warehouse.EquipmentID,
				EquipmentName: MesBasicInfo.warehouse.EquipmentName,
				FBDate: getBefore7DateTime(),
				FEDate: getFormatDateTime(new Date()),
			};
			that.assignFormData(that.FormData, formDefaults);
		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回

				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回

				return true;
			}
		},
		mounted() {

		},
		methods: {
			getList() {
				this.contents = []; // 假设这是异步获取数据的操作
				this.$refs.paging.reload();
			},
			getValue(payload) {
				var that = this;
				console.log(payload);
				that.showMLoading = false
				const {
					type,
					...data
				} = payload;
				if (payload === '返回') return;
				const typeHandlers = {
					'产品': () => ({
						ProductID: data.ProductID,
						ProductName: data.ProductName,
					})
				};
				if (typeHandlers[type]) {
					Object.assign(that.baseFormData, typeHandlers[type]());
				}
				console.log(this.baseFormData);
				this.$forceUpdate()
			},
			openBaseScreen(str) {
				console.log(str);
				var that = this;
				var obj;
				if (str == '产品') {
					obj = {
						cmd: 'getCPList',
						type: str,
						serverSearch: {
							isPaging: true, //是否分页
							pageIndexStr: 'PageIndex', //分页页码字段，应包含在param中		
							searchShow: false, //是否查询
							searchKey: 'searchKey', //搜索关键词，应包含在param中
							showText: 'ProductName', //查询结果显示的字段
						},
						param: {
							PageSize: 30,
							PageIndex: 1,
							Type: 'ZP'
						}
					}
				}
				console.log(obj);
				uni.navigateTo({
					url: `../common/baseScreen?obj=${JSON.stringify(obj)}`,
					animationType: 'slide-in-right',
					animationDuration: 300
				});
			},
			planIDClick(item, index) {
				let that = this;
				let routes = getCurrentPages();
				var url = `/st_mes_zs/abnormal/materialreviewDetail?ReviewID=${JSON.stringify(item.ReviewID)}`
				let obj = {
					path: 'manual',
					Name: '物料评审详情',
					APPFunID: '物料评审详情',
					length: routes.length + 1,
					url: url
				}
				this.GonavigateTo(obj, url)
			},

			addTime(name) {
				this.TimeName = name
				if (this.TimeName == '开始') {
					this.value1 = Number(getTimeStamp(this.FormData.FBDate))
				} else {
					this.value1 = Number(getTimeStamp(this.FormData.FEDate))
				}
				this.show = true
			},
			search() {
				this.$refs.popup.open('bottom')
			},
			submit() {
				let that = this;
				this.$refs.popup.close('bottom')
				this.contents = []; // 假设这是异步获取数据的操作
				this.$refs.paging.reload();

			},
			pickerConfirm(e) {
				let that = this;
				var time = getFormatDateTime(e.value)
				var formDefaults = {}
				if (this.TimeName == '开始') {
					this.FormData.FBDate = time
					formDefaults = {
						FBDate: time
					}
				} else {
					formDefaults = {
						FEDate: time
					}
				}
				that.assignFormData(that.FormData, formDefaults);
				this.show = false
			},
			queryList(pageNo) {
				let that = this;
				that.FormData.PageIndex = pageNo
				try {
					that.Request.request('getCKReviewList', that.FormData, (ret) => {
						console.log(ret);
						that.showMLoading = false
						if (ret.status == 200) {
							ret.data.ds.map((item) => {
								item.BillDate = formatDateTime(item.BillDate)
							})
							that.$refs.paging.complete(ret.data.ds);
						}
					}, (err) => {
						that.showMLoading = false
					})
				} catch (e) {
					console.log(e)
					that.showMLoading = false
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}
			},

			// 创建表单赋值工具函数
			assignFormData(target, defaults = {}) {
				var that = this;
				Object.keys(defaults).forEach(key => {
					target[key] = defaults[key];
				});
				that.$forceUpdate();
			},
			GonavigateTo(obj, url) {
				let that = this;
				var initData = JSON.parse(JSON.stringify(that.$store.state.GuyueModule.ModuleData_select));
				var parentData = initData[initData.length - 1]
				parentData.url = `/st_mes_zs/abnormal/materialreviewlist?Dispatchitem=${JSON.stringify(that.FormData)}`;
				that.$modifyParentData(parentData);
				this.$safeNavigate({
					targetUrl: url, // 目标页面路径
					payload: obj, // 要传递的数据
				});
			},
			async AddModuleData_select(res) {
				try {
					const backDelta = await this.GetModuleData_select(res);
				} catch (error) {
					console.error('返回操作失败:');
				}
			},
			changeLevel(list, data, row) {
				getchangeLevel(data)
			},

		}
	}
</script>

<style lang="scss" >
	@import "../css/index.css";
</style>